package com.example.overdue;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fresh.R;
import com.example.overdue.adapter.MyRecycleViewAdapter;
import com.example.overdue.util.DataUtil;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        //添加监听器，为年份下拉列表设置事件的响应
        ((Spinner) findViewById(R.id.spinner_year)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                //拿到被选择项的值
                String str = (String) ((Spinner) findViewById(R.id.spinner_year)).getSelectedItem();
                //修改年份的值
                setYear(str);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });

        ((Spinner) findViewById(R.id.spinner_year)).setSelection(0);

    }

    private void setYear(String year) {
        //获取列表控件
        RecyclerView rvShow  = findViewById(R.id.rv_show);
        // 设置布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvShow.setLayoutManager(linearLayoutManager);
        //为列表控件设置适配器
        rvShow.setAdapter(new MyRecycleViewAdapter(DataUtil.getOberdueList(),year));

    }
}
